'use strict'

const Service = require('egg').Service

class AssignService extends Service {

    /**
     * 查询接口service
     * @param offset
     * @param limit
     * @param search
     * @param isPage
     * @returns {Promise<*>}
     */
    async index(offset = 0,limit = 10,isPage = 'true',...args){
        const {app,ctx,service} = this;
        let sql = `
            select a.number,a.id,b.realname,b.province,b.address,b.phone,c.username
            from fd_order a
            left join fd_address b on a.address_id = b.id
            left join fd_user c on c.id = a.user_id
            where a.status = '0' and ( a.is_reservation = '0' or ( a.is_reservation = '1' and a.reservation_at <= now() ) )
        `;
        let count_sql = `
            select count(*) as total
            from fd_order a
            left join fd_address b on a.address_id = b.id
            where a.status = '0' and ( a.is_reservation = '0' or ( a.is_reservation = '1' and a.reservation_at <= now() ) )
        `;

        if(!app.lodash.isEmpty(args)){
            args = args[0];
            if(!app.lodash.isEmpty(args)){
                for(let key in args){
                    let value = args[key];
                    if(!app.lodash.isEmpty(value)){
                        console.info(key)
                        switch (key) {
                            case 'number':
                                sql += ` and a.number like '%${value}%' `;count_sql += ` and a.number like '%${value}%' `;break;
                            case 'realname':
                                sql += ` and b.realname like '%${value}%' `;count_sql += ` and b.realname like '%${value}%' `;break;
                            case 'phone':
                                sql += ` and b.phone like '%${value}%' `;count_sql += ` and b.phone like '%${value}%' `;break;
                        }
                    }
                }
            }
        }

        const sequelize_query_param = {raw:true,type:app.Sequelize.QueryTypes.SELECT};
        let data = await ctx.model.query(sql,sequelize_query_param);
        let count = await ctx.model.query(count_sql,sequelize_query_param);

        return {rows:data,count:count[0].total};
    }

    /**
     * 更新接口service
     * @param param
     * @returns {Promise<boolean>}
     */
    async update(param){
        const {app,ctx,service} = this;
        //开启事务
        let t = await ctx.model.transaction();
        try {
            //调用sequelize update更新为批量更新 只要满足where条件的数据都将被更新
            param.status = '1';
            let rs = await ctx.model.FdOrder.update(param,{where:{id:param.id},transaction:t,lock:t.LOCK.UPDATE});
            //rs返回影响的数据行数

            //提交事务
            t.commit();

            //判断结果
            if(rs[0] === 1){
                return true;
            }else{
                return false;
            }

        }catch (e) {
            //失败回滚
            t.rollback();
            //抛出错误信息
            ctx.throw(500,e.message)
        }
    }
}

module.exports = AssignService
